package com.asdan.seededu.web.controller;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.asdan.seededu.entity.security.Signin;
import com.asdan.seededu.service.SigninService;
import com.asdan.seededu.service.impl.SigninServiceImpl;
import com.asdan.seededu.util.MD5;

@Controller
@RequestMapping("signin")
public class SigninController {
	
	private static final String HttpResponse = null;
	@RequestMapping(value="/check",method=RequestMethod.POST)
	public ModelAndView check(String username,String password,Model model,HttpSession session ) {
		System.out.println( "username:"+ username + " password:"+password +" ") ;
		SigninService ss = new SigninServiceImpl() ;
		Signin sch = ss.getIdByEmailAndPassword(username,MD5.md5s(password),-1);
		
		System.out.println("验证后结果");
		System.out.println( "sch: "+sch.getName() + "email: "+sch.getEmail() + " uid:"+sch.getUid() +" password:"+sch.getPassword()) ;
		
		ModelAndView view = new ModelAndView();
		if(sch.getUid()==-1){
			model.addAttribute("message", "用户名不存在");
			view.setViewName("redirect:/sign_In.jsp");
	        return view;
		}else if(sch.getUid()==-2){
			model.addAttribute("message", "密码错误");
			view.setViewName("redirect:/sign_In.jsp");
	        return view;
		}else{
		session.setAttribute("userid", sch.getUid()+"");
		session.setAttribute("username", sch.getName()+"");
		model.addAttribute("signin",sch) ;
		view.setViewName("redirect:/index.jsp");
        return view;
		}
	}
	@RequestMapping(value="/signinOut",method=RequestMethod.GET)
	public String check(HttpSession session) {
		session.invalidate();
        return "redirect:/sign_In.jsp";
       // req.getRequestDispatcher().foward(HttpRequest request,HttpResponse response); 
		}
}
